//
//  UserAccModel.swift
//  weibo1
//
//  Created by 李进 on 15/10/17.
//  Copyright © 2015年 李进. All rights reserved.
//

import UIKit



class UserAccModel: NSObject ,NSCoding{
    //定义枚举
    enum SendMedoth1: String{
        
        case GET = "GET"
        case POST = "POST"
    }
    
    var access_token: String?
    
    var expires_in: NSTimeInterval = 0{
        didSet{
        
        expiresDate = NSDate(timeIntervalSinceNow: expires_in)
            
        }
    }
    
    var uid: String?
    
    var screen_name: String?
    
    var avatar_large: String?
    /// 过期日期
    var expiresDate: NSDate?
    
    
    //构造函数
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    //重写这个方法可以，使得可以动态转化成模型，不会报错
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
   //归档解档
    func saveData() {
        
        //userAccViewModel().userModel = self
        
        var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
        
        path = (path as NSString).stringByAppendingPathComponent("jjjjj.plist")
        
        //归档
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    }
    
    //归档
    // MARK: - `键值`归档和解档
    /// 归档 - 在把当前对象保存到磁盘前，将对象编码成二进制数据 － 跟网络的序列化概念很像！
    ///
    /// - parameter aCoder: 编码器
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    /// 解档 - 从磁盘加载二进制文件，转换成对象时调用 － 跟网络的反序列化很像
    ///
    /// - parameter aDecoder: 解码器
    ///
    /// - returns: 当前对象
    /// `required` - 没有继承性，所有的对象只能解档出当前的类对象
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}


